package evi.com.evi.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import evi.com.evi.utils.OrderStatus;

/**
 * Created by YX_BOSS on 2017/3/10.
 */

public class DisposeOrderBean {
    private String mainOrderId; //订单编号
    private String order_time;  //上方时间
    private String order;  //订单号
    private String state;  //状态
    private Integer orderStatus;//状态编码
    private Integer orderCancelStatus;//订单取消状态
    private String time;  //出发时间
    private List<SubOrderDetailBean> foodList;  //子订单列表
    private String name;  //收货人
    private String telnum;  //手机号
    private Integer deliverType;//配送方式 16002配送 16001自提
    private String type;  //运货方式
    //    private String type_name;  //配送员
    private String reason;  //原因
    private String refuse;  //拒绝原因
    private String money;  //金钱
    private String serviceProviderName; //配送员姓名
    private String serviceProviderPhoneNo; //配送员电话

    public DisposeOrderBean() {

    }

    private String getType(Integer type) {
        if (type != null) {
            if (type == 16002) {
                return "配送员送";
            } else if (type == 16001) {
                return "到店自取";
            }
        }
        return "未知";
    }


    private String getStatus(Integer status, Integer cancelStatus) {
        if (cancelStatus != null && cancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
            return "用户申请取消订单";
        }
        if (status != null) {
            if (status == OrderStatus.MAINORDER_STATUS_WAIT_ACCEPT) {
                return "等待商家接单";
            } else if (status == OrderStatus.MAINORDER_STATUS_ACCEPT) {
                return "商家已接单";
            } else if (status == OrderStatus.MAINORDER_STATUS_REJECT) {
                return "商家拒绝接单";
            } else if (status == OrderStatus.MAINORDER_STATUS_SERVICER_ACCEPT) {
                return "服务员已接单";
            } else if (status == OrderStatus.MAINORDER_STATUS_SERVICER_GET_GOOD) {
                return "服务员已取货";
            } else if (status == OrderStatus.MAINORDER_STATUS_COMPLETED) {
                return "订单已完成";
            } else if (status == OrderStatus.MAINORDER_STATUS_CANCELED) {
                return "订单已取消";
            } else if (status == OrderStatus.MAINORDER_STATUS_EXPIRE) {
                return "订单已失效";
            } else if (status == OrderStatus.MAINORDER_STATUS_OVERTIME) {
                return "订单已超时";
            } else if (status == OrderStatus.MAINORDER_STATUS_USER_GET_GOOD) {
                return "等待用户取货";
            }
        }
        return "未知";
    }

    public String getString(String str) {
        if (str == null) {
            return "";
        }
        if (str.equals("null")) {
            return "";
        }

        return str;
    }

    public DisposeOrderBean(JSONObject json) {
        this.mainOrderId = json.getString("mainOrderId");
        this.order_time = json.getString("updateDate");
        this.order = "订单号：" + json.getString("orderNo");
        this.state = "状态：" + getStatus(json.getInteger("orderStatus"), json.getInteger("orderCancelStatus"));
        if (json.getInteger("deliverType") == 16001) {
            this.time = "预计取货时间：" + getString(json.getString("latestServiceTime").substring(11, 16));
        } else {
            this.time = "最迟送达时间：" + getString(json.getString("latestServiceTime").substring(11, 16));
        }

        this.name = "收货人：" + getString(json.getString("customerName"));

        this.telnum = "手机号：" + getString(json.getString("customerPhoneNo"));
        this.deliverType = json.getInteger("deliverType");
        this.type = "送货方式：" + getType(json.getInteger("deliverType"));
        this.orderCancelStatus = json.getInteger("orderCancelStatus");
//        this.type_name = "配送员："+ getString(json.getString("serviceProviderName"));

        if (json.getString("serviceProviderName") == null || json.getString("serviceProviderName").equals("null")) {
            this.serviceProviderName = "配送员：暂未指定";
        } else {
            this.serviceProviderName = "配送员：" + getString(json.getString("serviceProviderName"));
        }
        this.serviceProviderPhoneNo = "配送员电话：" + getString(json.getString("serviceProviderPhoneNo"));


        this.money = "¥" + json.getString("orderTotalPrice");

        this.reason = "取消原因：" + getString(json.getString("cancelReason"));
        this.refuse = "拒绝原因：" + getString(json.getString("refuseReason"));
        this.orderStatus = json.getInteger("orderStatus");

        foodList = new ArrayList<>();
        JSONArray subs = json.getJSONArray("subOrders");
        if (subs != null) {
            JSONObject sub;
            for (int i = 0; i < subs.size(); i++) {
                sub = subs.getJSONObject(i);
                foodList.add(JSONObject.toJavaObject(sub, SubOrderDetailBean.class));
            }
        }

    }

//    public DisposeOrderBean(String order_time,String order, String state, String time, List<String> foodList, String name, String telnum, String type, String type_name, String reason, String refuse, String money) {
//        this.order_time = order_time;
//        this.order = order;
//        this.state = state;
//        this.time = time;
////        this.foodList = foodList;
//        this.name = name;
//        this.telnum = telnum;
//        this.type = type;
//        this.type_name = type_name;
//        this.reason = reason;
//        this.refuse = refuse;
//        this.money = money;
//    }

    public String getMainOrderId() {
        return mainOrderId;
    }

    public void setMainOrderId(String mainOrderId) {
        this.mainOrderId = mainOrderId;
    }

    public Integer getOrderCancelStatus() {
        return orderCancelStatus;
    }

    public void setOrderCancelStatus(Integer orderCancelStatus) {
        this.orderCancelStatus = orderCancelStatus;
    }

    public Integer getDeliverType() {
        return deliverType;
    }

    public void setDeliverType(Integer deliverType) {
        this.deliverType = deliverType;
    }

    public String getServiceProviderName() {
        return serviceProviderName;
    }

    public void setServiceProviderName(String serviceProviderName) {
        this.serviceProviderName = serviceProviderName;
    }

    public String getServiceProviderPhoneNo() {
        return serviceProviderPhoneNo;
    }

    public void setServiceProviderPhoneNo(String serviceProviderPhoneNo) {
        this.serviceProviderPhoneNo = serviceProviderPhoneNo;
    }

    public String getOrder_time() {
        return order_time;
    }

    public void setOrder_time(String order_time) {
        this.order_time = order_time;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public List<SubOrderDetailBean> getFoodList() {
        return foodList;
    }

    public void setFoodList(List<SubOrderDetailBean> foodList) {
        this.foodList = foodList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelnum() {
        return telnum;
    }

    public void setTelnum(String telnum) {
        this.telnum = telnum;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

//    public String getType_name() {
//        return type_name;
//    }
//
//    public void setType_name(String type_name) {
//        this.type_name = type_name;
//    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getRefuse() {
        return refuse;
    }

    public void setRefuse(String refuse) {
        this.refuse = refuse;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public Integer getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(Integer orderStatus) {
        this.orderStatus = orderStatus;
    }

    @Override
    public String toString() {
        return "DisposeTrueBeanNeedDelete{" +
                "order='" + order + '\'' +
                ", state='" + state + '\'' +
                ", time='" + time + '\'' +
                ", foodList=" + foodList +
                ", name='" + name + '\'' +
                ", telnum='" + telnum + '\'' +
                ", type='" + type + '\'' +
//                ", type_name='" + type_name + '\'' +
                ", reason='" + reason + '\'' +
                ", refuse='" + refuse + '\'' +
                ", money='" + money + '\'' +
                '}';
    }
}
